<?php

class PointsController extends AppController {
  var $paginate = array(
    'Point' => array(
      /*'conditions' => array('Point.visible' => 1), */
      'order' => 'Point.created DESC',
      'limit' => 10,
    ));

  function home() {
    $this->data = $this->Session->read('me');
    $this->setCaptcha();
  }
  
  function index() {
    $this->set('truncate', $this->theme == 'bp01' ? 80 : 100);
    $this->set('points', $this->paginate('Point'));
  }
  
  function add() {
    if ($this->data) {
      $destination = array('action' => 'home');
      
      if (!$this->account && !$this->checkCaptcha()) {
        $this->setError('Bitte löse die Rechenaufgabe.', array('action' => 'home'));
      }

      /*
       * Handle recipient
       */
      if (@$this->data['Point']['user_id']) {
        $destination = array('controller' => 'users', 'action' => 'view', $this->data['Point']['user_id']);
      }
      else
      if ($recipient_id = $this->User->field('id', array('User.email' => $this->data['Point']['email']))) {
        $this->data['Point']['user_id'] = $recipient_id;
      }
      else {
        if ($this->User->save($this->data['Point'])) {
          $this->data['Point']['user_id'] = $this->User->getInsertID();
        }
        else {
          $this->setError(join('<br />', $this->User->validationErrors), $destination);
        }
      }
      
      /*
       * Handle sender
       */
      if ($this->account) {
        $this->data['Point']['sender_id'] = $this->account['User']['id'];
      }

      /*
       * Save point
       */
      if ($this->Point->save($this->data)) {
        $this->Session->write('me', $this->data);
        
        /*
         * Reflect on Twitter
         */
        if ($recipient_twitter = $this->TwitterAccount->field('screen_name', array('user_id' => $this->data['Point']['user_id']))) {
          $to = 'für @' . $recipient_twitter . ': ';
        }
        else
        if ($recipient_name = $this->User->field('name', array('id' => $this->data['Point']['user_id']))) {
          $to = 'für #' . preg_replace('/\W+/', '_', trim($recipient_name) . ': ');
        }
        else {
          $to = '';
        }
        $this->_update_status("{$this->data['Point']['text']}");
        
        $this->setSuccess('Der Browniepunkt wurde verschickt.', $destination);
      }
      else {
        $this->setError(join('<br />', $this->Point->validationErrors), $destination);
      }
    }
    $this->redirect($destination);
  }
  
  private function _update_status($msg) {
    $browniepoint = $this->TwitterAccount->find('first', array(
      'conditions' => array('screen_name' => 'browniepunkt'),
      'fields' => array('oauth_access_token', 'oauth_access_token_secret')));
    error_log("TWITTER {$msg}: " . json_encode($this->TwitterAuth->update_status($msg, $browniepoint['TwitterAccount'])));
  }
  
  function claim($code) {
    $point = $this->Point->find('first', array(
      'conditions' => array('code' => $code)));
    
    if (!$point) {
      $this->setError('Diesen Browniepunkt gibt es nicht.', array('action' => 'home'));
    }

    if (!$point['Point']['visible']) {
      if ($this->account) {
        $this->Point->set('id', $point['Point']['id']);
        if ($this->Point->save(array(
          'visible' => 1, 
          'code' => NULL,
          'user_id' => $this->account['User']['id'],
        ))) {
          $this->setSuccess('Ein Punkt für dich!', array('controller' => 'users', 'action' => 'view', $this->account['User']['id']));
        }
        else {
          $this->setError(join('<br />', $this->Point->validationErrors), 
            array('controller' => 'users', 'action' => 'view', $this->account['User']['id']));        
        }
      }
      else {
        $this->Session->write('destination', $this->here);
        $this->setError('Bitte melde dich an, um diesen Punkt einzusammeln.', array('controller' => 'users', 'action' => 'login'));
      }
    }
    $this->redirect(array('controller' => 'users', 'action' => 'view', $point['Point']['user_id']));
  }
}
